home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / share / onboard / utils.py < prev    next >
Encoding:
Python Source  |  2007-02-20  |  6.4 KB  |  222 lines

  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import os
  5. from xml.dom import minidom
  6. from copy import deepcopy
  7. from xml.dom.ext import PrettyPrint
  8. from Key import * 
  9.  
  10. def run_script(script,sok):
  11.         a =__import__(script)
  12.         
  13.         a.run(sok)
  14.  
  15.  
  16. modifiers = {"shift":1,"caps":2, "control":4, "mod1":8, "mod2":16, "mod3":32, "mod4":64, "mod5":128}
  17.  
  18.  
  19. modDic = {"LWIN" : ("Win",64),"RTSH" : ("Γçº".decode('utf-8'), 1), "LFSH" : ("Γçº".decode('utf-8'), 1), "RALT" : ("Alt Gr", 128), "LALT" : ("Alt", 8), "RCTL" : ("Ctrl", 4), "LCTL" : ("Ctrl", 4), "CAPS" : ("CAPS", 2), "NMLK" : ("Nm\nLk",16)}
  20.  
  21. otherDic = {"RWIN" : "Win","MENU" : "Menu" ,"BKSP" : "Γçª".decode("utf-8"),"RTRN" : "Return", "TAB" : "Tab", "INS":"Ins", "HOME":"Hm", "PGUP": "Pg\nUp","DELE":"Del","END":"End","PGDN":"Pg\nDn", "UP":  "Γåæ".decode("utf-8"), "DOWN":"Γåô".decode("utf-8"), "LEFT" : "ΓåÉ".decode("utf-8"), "RGHT" : "ΓåÆ".decode("utf-8"), "KP0" : "0", "KP1" : "1", "KP2" : "2", "KP3" : "3", "KP4" : "4", "KP5" : "5", "KP6" : "6", "KP7" : "7", "KP8" : "8", "KP9" : "9", "KPDL":"Del", "KPEN": "Ent" }
  22.  
  23. funcKeys = (("ESC",65307),("F1",65470),("F2",65471),("F3",65472),("F4", 65473),("F5", 65474),("F6",65475),("F7",65476),("F8",65477),("F9",65478),("F10",65479),("F11", 65480),("F12", 65481),
  24.             ("Prnt", 65377), ("Scroll", 65300),("Pause", 65299))
  25.             
  26.             
  27. keysyms = {"space" : 65408, "insert" : 0xff9e, "home" : 0xff50, "page_up" : 0xff55, "page_down" : 0xff56, "end" :0xff57, "delete" : 0xff9f, "return" : 65293, "backspace" : 65288}
  28.  
  29.  
  30.  
  31. def create_default_layout_XML(name,vk,sok):
  32.     "Reads layout stored within onBoard and outputs it to XML"
  33.     doc = minidom.Document()
  34.     
  35.     
  36.     keyboard_element = doc.createElement("keyboard")
  37.     keyboard_element.setAttribute("id", name)
  38.     doc.appendChild(keyboard_element)
  39.     
  40.     
  41.     f = open(os.path.join(sok.SOK_INSTALL_DIR,"layouts","template.svg"))
  42.     baseDoc = minidom.parse(f)
  43.     f.close()
  44.     
  45.     paneDocs = []
  46.     for pane in sok.keyboard.panes:
  47.         paneDoc = deepcopy(baseDoc)
  48.         paneDocs.append((pane,paneDoc))#tuple
  49.     
  50.     pane = sok.keyboard.basePane
  51.     
  52.     read_layout_from_sok(doc, baseDoc, pane, vk,name)
  53.     
  54.     for paneDoc in paneDocs:
  55.         read_layout_from_sok(doc, paneDoc[1], paneDoc[0], vk,name)
  56.             
  57.     
  58.     #messy
  59.     docFile = open(os.path.join(os.path.expanduser("~"), ".sok", "layouts", "%s.sok" % name), 'w')
  60.     PrettyPrint(doc,docFile)
  61.     docFile.close()
  62.     
  63.     docFile = open(os.path.join(os.path.expanduser("~"), ".sok", "layouts", "%s-%s.svg" % (name,sok.keyboard.basePane.ident)), 'w')
  64.     PrettyPrint(baseDoc,docFile)
  65.     docFile.close()
  66.     
  67.     for pane in paneDocs:
  68.         docFile = open(os.path.join(os.path.expanduser("~"), ".sok", "layouts", "%s-%s.svg" % (name,pane[0].ident)), 'w')
  69.         PrettyPrint(pane[1],docFile)
  70.         docFile.close()
  71.             
  72.                                                     
  73.     
  74. def read_layout_from_sok(doc,svgDoc, pane, vk, name):
  75.     
  76.     basePane_element  = make_xml_pane(doc,pane.ident,"%s-%s.svg" % (name,pane.ident),pane.rgba,pane.fontSize)
  77.     doc.documentElement.appendChild(basePane_element)
  78.     
  79.     svgDoc.documentElement.setAttribute("width", str(pane.viewPortSizeX))
  80.     svgDoc.documentElement.setAttribute("height", str(pane.viewPortSizeY))
  81.     
  82.     print 2
  83.     
  84.     for keyKey,keyVal in pane.keys.items():
  85.         if keyVal.__class__ == RectKey:
  86.             svgDoc.documentElement.appendChild(make_xml_rect(doc,
  87.                                             keyKey,
  88.                                             keyVal.x,
  89.                                             keyVal.y,
  90.                                             keyVal.width,
  91.                                             keyVal.height,
  92.                                             keyVal.rgba))
  93.         
  94.             
  95.             labels = ['','','','','']        
  96.             for n in range(len(keyVal.labels)):
  97.                 try:
  98.                     labels[n] = keyVal.labels[n].decode('utf-8')
  99.                     
  100.                 except UnicodeDecodeError:
  101.                     labels[n] = '?' #to deal with xorg 7.1 which seems to report some wonky values...
  102.  
  103.             basePane_element.appendChild(make_xml_key(doc,
  104.                                             keyKey, 
  105.                                             labels, 
  106.                                             keyVal.actions,
  107.                                             keyVal.sticky,
  108.                                             keyVal.fontOffsetX,
  109.                                             keyVal.fontOffsetY))
  110.             
  111.         elif keyVal.__class__ == LineKey:
  112.             print "funky keys not yet implemented"
  113.             
  114.         
  115.  
  116. def make_xml_pane(doc,ident,filename,rgba,font):        
  117.     
  118.     pane_element = doc.createElement("pane")
  119.     
  120.     pane_element.setAttribute("id", ident)
  121.     pane_element.setAttribute("filename", filename)
  122.     pane_element.setAttribute("backgroundRed", str(rgba[0]))
  123.     pane_element.setAttribute("backgroundGreen", str(rgba[1]))
  124.     pane_element.setAttribute("backgroundBlue", str(rgba[2]))
  125.     pane_element.setAttribute("backgroundAlpha", str(rgba[3]))
  126.     pane_element.setAttribute("font", str(font))
  127.     
  128.     
  129.     return pane_element
  130.     
  131. def make_xml_rect(doc,ident,x,y,width,height,rgba):
  132.     rect_element = doc.createElement("rect")
  133.         
  134.     rect_element.setAttribute("id",ident)
  135.     rect_element.setAttribute("x",str(x))
  136.     rect_element.setAttribute("y",str(y))
  137.     rect_element.setAttribute("width",str(width))
  138.     rect_element.setAttribute("height",str(height))
  139.  
  140.     rect_element.setAttribute("style","fill:#%s%s%s;stroke:#000000;" % (dec_to_hex_colour(rgba[0]),
  141.                                                             dec_to_hex_colour(rgba[1]),dec_to_hex_colour(rgba[2])))
  142.     
  143.     return rect_element
  144.  
  145. def dec_to_hex_colour(dec):
  146.     
  147.     hexString = hex(int(255*dec))[2:]    
  148.     if len(hexString) == 1:
  149.         hexString = "0" + hexString
  150.         
  151.     
  152.     return hexString
  153.         
  154.  
  155.  
  156. def make_xml_key(doc,ident, labels, actions,sticky, fontOffsetX, fontOffsetY):
  157.     key_element = doc.createElement("key")
  158.     
  159.     if labels[0]:
  160.         key_element.setAttribute("label",labels[0])
  161.     if labels[1]:
  162.         key_element.setAttribute("cap_label",labels[1])
  163.     if labels[2]:
  164.         key_element.setAttribute("shift_label",labels[2])
  165.     if labels[3]:
  166.         key_element.setAttribute("altgr_label",labels[3])
  167.     if labels[4]:
  168.         key_element.setAttribute("altgrNshift_label",labels[4])
  169.     
  170.     
  171.     key_element.setAttribute("id",ident)
  172.         
  173.     
  174.     if actions[0]:
  175.         key_element.setAttribute("char", actions[0])
  176.     elif actions[1]:
  177.         key_element.setAttribute("keysym", str(actions[1]))
  178.     elif actions[2]:
  179.         key_element.setAttribute("press", actions[2])
  180.     elif actions[3]:
  181.         for key,val in modifiers.items():
  182.             if actions[3] == val:
  183.                 key_element.setAttribute("modifier", key)
  184.         
  185.     elif actions[4]:
  186.         key_element.setAttribute("macro", actions[4])
  187.     elif actions[5]:
  188.         key_element.setAttribute("script", actions[5])
  189.     
  190.     if fontOffsetX:
  191.         key_element.setAttribute("font_offset_x", fontOffsetX)
  192.     
  193.     if fontOffsetY:
  194.         key_element.setAttribute("font_offset_y", fontOffsetY)
  195.     
  196.     
  197.     if sticky:
  198.         key_element.setAttribute("sticky", "true")
  199.     else:
  200.         key_element.setAttribute("sticky", "false")    
  201.     
  202.     
  203.     return key_element
  204.  
  205.  
  206.             
  207. if __name__=='__main__':
  208.     
  209.     from sys import argv
  210.     
  211.     
  212.     if argv[0]:
  213.         from virtkey import virtkey
  214.         from sok import Sok
  215.         s = Sok()
  216.         vk = virtkey()
  217.         create_default_layout_XML(argv[0],vk,s)
  218.     else:
  219.         print "Type name for personalised layout"
  220.     s.clean
  221.     
  222.